package com.xujian.flink.split;

import com.xujian.common.beans.EventSplit;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Calendar;
import java.util.Random;
/**
 * @author star xu
 * @date 2023/3/31 15:59
 * @Description:
 * @Slogan: 致敬大师,致敬未来的自己
 */
public class MySource implements SourceFunction<EventSplit> {
        //是否执行
        private boolean ifRun = true;

        @Override
        public void run(SourceContext<EventSplit> ctx) throws Exception {
            Random random = new Random();
            String[] users = {"令狐冲", "依琳", "莫大", "风清扬", "任盈盈", "林远图", "定仪"};
            String[] urls = {"/home", "/cart", "/pay", "/info?id?"};
            while (ifRun) {
                //直接返回一个对象
                ctx.collect(new EventSplit(users[random.nextInt(urls.length)],
                        urls[random.nextInt(urls.length)],
                        Calendar.getInstance().getTimeInMillis()));
                //1s  生成一条
                Thread.sleep(1000);
            }
        }

        @Override
        public void cancel() {
            //取消
            ifRun = false;
        }
    }